package com.xjtu.platform.reader.dao;

import com.xjtu.platform.reader.dto.ReturnBookDto;
import com.xjtu.platform.reader.entity.BookScore;
import com.xjtu.platform.reader.entity.PenaltyUsingTime;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface BorrowDao {

    @Update("update reader_book_borrow_his set end_time = #{time}, borrow_state = '1' " +
            "where book_reader_his_id = #{dto.book_reader_his_id} " +
            "and borrow_state = '0'")
    void updateBorrowHis(@Param("dto") ReturnBookDto returnBookDto, @Param("time") Timestamp end_time);


    @Update("update book set current_num = cast(current_num as unsigned) + cast(#{num} as unsigned) where book_id = #{book_id}")
    void returnBookNum(ReturnBookDto returnBookDto);

    @Insert("insert into book_score_his values(#{dto.book_reader_his_id},#{dto.book_id},null,#{dto.score},#{time},'1')")
    void scoreBook(@Param("dto") ReturnBookDto returnBookDto, @Param("time") Timestamp end_time);


    @Select("select score from book_score_his where book_id = #{bookId}")
    List<BookScore> getBookScoreList(String bookId);

    @Update("update book set total_score = #{score} where book_id = #{id}")
    void updateBookScore(@Param("id") String bookId, @Param("score") String newScore);

    @Select("select start_time,borrow_time from reader_book_borrow_his where book_reader_his_id = #{bookReaderHisId}")
    PenaltyUsingTime getStartTime(String bookReaderHisId);

    @Insert("insert into user_penalty values(null,#{money},#{time},#{id})")
    void insertUserPenalty(@Param("id") String bookReaderHisId, @Param("money") String money, @Param("time") Timestamp endTime);
}
